package jsyf.oa.dao.psi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import jsyf.oa.entity.psi.GoodsEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

/**
 * Created by Maqiuqiu on 2018/10/17.
 */
@Repository
public class GoodsDao extends BaseDao {

    public GoodsDao() {
        super.iBaseEntity = new GoodsEntity();
    }

    public List<GoodsEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<GoodsEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            GoodsEntity entity = (GoodsEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(entity);
        }
        return entityList;
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, GoodsEntity.class);
    }

    public List<JSONObject> getGoodsList(String condition, Integer requestPage, Integer pageSize){
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT\n" +
                "  t_psi_goods.ID AS id,\n" +
                "  t_psi_goods.`NAME` AS goodsName,\n" +
                "  t_psi_goods.UNIT AS goodsUnit,\n" +
                "  t_psi_goods.SPEC AS goodsSpec,\n" +
                "  t_psi_goods.UNIT_PRICE AS goodsUnitPrice,\n" +
                "  t_psi_goods.DESCRIPTION AS goodsDescription,\n" +
                "  t_psi_goods.IMAGE AS goodsImage,\n" +
                "  t_psi_goods.REMARK AS goodsRemark,\n" +
                "  t_psi_goods.EXTRA AS goodsExtra,\n" +
                "  t_psi_goods.CREATE_TIME AS createTime,\n" +
                "  t_psi_goods.CREATE_USER_ID AS userId,\n" +
                "  t_sys_user.`NAME` AS userName,\n" +
                "  t_sys_role_name.`NAME` AS roleName,\n" +
                "  t_sys_depart.ID AS departId,\n" +
                "  t_sys_depart.`NAME` AS departName,\n" +
                "  t_sys_company.ID AS companyId,\n" +
                "  t_sys_company.`NAME` AS companyName\n" +
                "FROM t_psi_goods\n" +
                "LEFT JOIN t_sys_user ON t_psi_goods.CREATE_USER_ID = t_sys_user.ID  \n" +
                "LEFT JOIN t_sys_role ON t_sys_user.ROLE_ID = t_sys_role.ID\n" +
                "LEFT JOIN t_sys_role_name ON t_sys_role.ROLE_NAME_ID = t_sys_role_name.ID\n" +
                "LEFT JOIN t_sys_depart ON t_sys_role.DEPART_ID = t_sys_depart.ID\n" +
                "LEFT JOIN t_sys_company ON t_sys_depart.COMPANY_ID = t_sys_company.ID\n" +
                "WHERE 1 = 1 ");
        if (condition != null){
            sql.append(condition);
        }
        super.appendPageSql(sql, requestPage, pageSize);
        return super.queryForJsonList(sql.toString());
    }

    public JSONObject getInventoryGoodsInfo(String goodsId){
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT\n" +
                "\tt_psi_goods.ID AS goodsId,\n" +
                "\tt_psi_goods.`NAME` AS goodsName,\n" +
                "\tt_psi_goods.spec AS goodsSpec,\n" +
                "\tt_psi_goods.UNIT AS goodsUnit,\n" +
                "\tt_psi_goods.UNIT_PRICE AS goodsPrice,\n" +
                "\tt_psi_goods.REMARK AS goodsRemark,\n" +
                "\tt_psi_purchase.APPLY_DATE AS applyDate,\n" +
                "\tt_psi_purchase.COMPANY_NAME AS companyName,\n" +
                "\tt_psi_purchase.CREATE_USER_NAME AS purchaseUserName,\n" +
                "\tt_psi_purchase.METHOD AS method,\n" +
                "\tt_psi_purchase.IN_REPO_DATE AS inRepoDate,\n" +
                "\tt_psi_purchase.IN_USER_NAME AS inUserName,\n" +
                "\tt_psi_inventory.ADDRESS AS address,\n" +
                "\tt_psi_repo_in_out.ID AS repoId\n" +
                "FROM\n" +
                "\tt_psi_inventory\n" +
                "LEFT JOIN t_psi_goods ON t_psi_goods.ID = t_psi_inventory.GOODS_ID\n" +
                "LEFT JOIN t_psi_purchase ON t_psi_inventory.PURCHASE_ID = t_psi_purchase.ID\n" +
                "LEFT JOIN t_psi_repo_in_out ON t_psi_inventory.REPO_IN_OUT_ID = t_psi_repo_in_out.ID\n" +
                "WHERE 1 = 1\n" +
                "AND t_psi_goods.ID = '"+goodsId+"'\n" +
                "AND t_psi_purchase.STATE = '3' ");
        return super.queryForJsonObject(sql.toString());
    }

    public List<JSONObject> getGoodsInfoByName(String condition, Object[] params) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT\n" +
                "  t_psi_goods.ID AS id,\n" +
                "  t_psi_goods.`NAME` AS goodsName,\n" +
                "  t_psi_goods.UNIT AS goodsUnit,\n" +
                "  t_psi_goods.SPEC AS goodsSpec,\n" +
                "  t_psi_goods.UNIT_PRICE AS goodsUnitPrice,\n" +
                "  t_psi_goods.DESCRIPTION AS goodsDescription,\n" +
                "  t_psi_goods.IMAGE AS goodsImage,\n" +
                "  t_psi_goods.REMARK AS goodsRemark,\n" +
                "  t_psi_goods.EXTRA AS goodsExtra,\n" +
                "  t_psi_goods.CREATE_TIME AS createTime,\n" +
                "  t_psi_goods.CREATE_USER_ID AS userId\n" +
                "FROM t_psi_goods\n" +
                "WHERE 1 = 1 ");
        sql.append(condition);
        sql.append(" LIMIT " + 0 + "," + 10);
        List<JSONObject> listResult = super.queryForJsonList(sql.toString(), params);
        return listResult;
    }
}

